package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 江西高考查询
 * 排名:完成(已经又本科排名，和专科排名)
 * 去年排名：完成
 * 
 * @author chenmin <chenmin@mouge.wang>
 * @since 2015-06-23
 */
public class Jiangxi extends Exam implements IRank
{

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("452820916");
		student.setStudentPwd("2073");
		Jiangxi guangxi = new Jiangxi(student);
		System.out.println(guangxi.login(""));
	}

	public Jiangxi(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request initRequest = new Request("http://gkcf.jxedu.gov.cn/cjcx/cjcx.html", mSha1);
		Document initDoc = Jsoup.parse(curlGet(initRequest));

		Request login = new Request("http://gkcf.jxedu.gov.cn/cjcx/CjQuerySelvet", mSha1);
		login.addParams("dmlx", initDoc.select("input[name=dmlx]").val());
		login.addParams("va", initDoc.select("input[name=va]").val());
		login.addParams("year", initDoc.select("input[name=year]").val());
		login.addParams("code", mStudent.getStudentId());
		login.addParams("ct", "2");// 准考证号查询
		login.addParams("sfzh", mStudent.getStudentPwd());
		login.addParams("yzm", createCheckCode());
		login.addParams("action.x", "59");
		login.addParams("action.y", "8");

		String loginHtml = curlPost(login);

		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("考生姓名"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("准考证号不存在"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("身份证号后四位错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDoc = Jsoup.parse(parseHtml);
		Elements trs = loginDoc.select("table").get(6).select("tr");

		JSONObject data = new JSONObject();
		data.put("exam_id", mStudent.getStudentId());
		data.put("name", regexMatch(trs.get(1).text(), "：(.*)"));
		data.put("ranking", "");
		data.put("province", "江西省");

		Elements subTrs = trs.get(5).select("table tr");

		JSONObject score = new JSONObject();
		score.put("语文", regexMatch(subTrs.get(1).select("td").get(0).text(), "(.*)\u5206"));
		score.put("数学", regexMatch(subTrs.get(1).select("td").get(1).text(), "(.*)\u5206"));
		score.put("外语", regexMatch(subTrs.get(1).select("td").get(2).text(), "(.*)\u5206"));
		score.put("综合", regexMatch(subTrs.get(1).select("td").get(3).text(), "(.*)\u5206"));
		score.put("技术", regexMatch(subTrs.get(1).select("td").get(4).text(), "(.*)\u5206"));
		score.put("本科总分(含加分)", regexMatch(trs.get(3).select("td").get(0).text(), "：(.*)"));
		score.put("专科总分(含加分)", regexMatch(trs.get(4).select("td").get(0).text(), "：(.*)"));
		score.put("本科排名", regexMatch(trs.get(3).select("td").get(1).text(), "：(.*)"));
		score.put("专科排名", regexMatch(trs.get(4).select("td").get(1).text(), "：(.*)"));

		data.put("score", score.toString());

		return data.toString();
	}

	private String createCheckCode()
	{
		char selectChar[] = new char[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
				'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

		String codevalue = "";
		int codeLenght = 6;
		for (int i = 0; i < codeLenght; i++)
		{
			double charIndex = Math.floor(Math.random() * 36);
			codevalue += selectChar[(int) charIndex];
		}

		return codevalue;
	}

	@Override
	public void initRank()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(17);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2014/107408.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/107408_3.html"));
		// 文科 http://www.gxeduw.com/news/2014/107409.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 142635;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(2).text(), "(\\d+)");

			if (countString.equals(""))
				continue;
			Integer count = Integer.parseInt(countString);
			Integer ranking = pre + 1;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_LI_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
			System.out.println(rank);
		}

	}

}
